package org.u2ml.app.business.impl;

import java.util.List;

import javax.ws.rs.QueryParam;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.u2ml.app.business.AttributeService;
import org.u2ml.app.domain.Attribute;

@Component
@Transactional
public class AttributeServiceImpl implements AttributeService {

	@Autowired
	private SessionFactory sessionFactory;
	
	public void delete(Attribute attribute) {
		sessionFactory.getCurrentSession().delete(attribute);
	}

	public Attribute save(Attribute attribute) {
		sessionFactory.getCurrentSession().save(attribute);
		return attribute;
	}

	public Attribute update(Attribute attribute) {
		sessionFactory.getCurrentSession().update(attribute);
		return attribute;
	}

	@SuppressWarnings("unchecked")
	public Attribute find(@QueryParam("oid") Long oid) {
		List<Attribute> list = sessionFactory.getCurrentSession()
        	.createQuery("from Attribute attr where attr.oid = :oid")
        	.setParameter("oid", oid)
        	.list();
		return (list.size() == 0) ? null : list.get(0);
	}

}
